﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Zgq.CommonTools;

namespace Zgq.WpfTools
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded -= MainWindow_Loaded;
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.tbxBaseClassName.Text = BaseClass.BaseClassName;
            this.tbxDirectoryPath.Text = BaseClass.DirectoryPath;
            this.tbxNameSpace.Text = BaseClass.NameSpace;
            InitBindDataSource();
        }

        DataTable dtSource = null;
        public void InitBindDataSource()
        {
            var dtName = this.tbxTbName.Text;
            string whereStr = null;
            if (!string.IsNullOrEmpty(dtName))
                whereStr = $"AND s.Name like '%{dtName}%'";
            string sqlStr = @$"SELECT s.Name,Convert(varchar(max),tbp.value) as Description, s.xtype
                            FROM sysobjects s
                             LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0  AND (tbp.Name='MS_Description' OR tbp.Name is null) 
                             WHERE s.xtype IN('U','V')
                                {whereStr}
							 ORDER BY s.Name
                            ";
            dtSource = SqlHelper.GetTable(BaseClass.ConnStr, sqlStr, null);

            this.dgDbInfo.ItemsSource = dtSource.AsDataView();
        }


        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            InitBindDataSource();
        }

        private void btnCreateModel_Click(object sender, RoutedEventArgs e)
        {
            var tableName = (this.dgDbInfo.SelectedItem as DataRowView)["Name"].ToStringExt();

            string sqlStr = @$"SELECT d.name as  tableName,a.name as FieldName,case COLUMNPROPERTY( a.id,a.name,'IsIdentity') when 1 then 'true' when 0 then 'false' end  as AutoIncrement, 
                            CASE WHEN EXISTS(SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id
                            AND sc.colid = sik.colid INNER JOIN dbo.sysobjects so ON so.name = si.name AND so.xtype = 'PK' WHERE sc.id = a.id AND sc.colid = a.colid) THEN 'true' ELSE 'false' END AS PrimaryKey,
		                    b.name as DbType,a.length as MaxLength, a.xprec as Precision,a.xscale as Scale,'false' as IsNotModif,case b.xtype when 189 then 'true' else 'false' end as IsTimeStamp,
		                    a.isnullable as IsNullable,ISNULL(g.value, '') as ReMark
                            FROM syscolumns a left join systypes b on a.xtype = b.xusertype inner join sysobjects d on a.id = d.id and (d.xtype = 'U' or d.xtype = 'V') and d.name <> 'dtproperties'
                            LEFT JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id

                            where d.name='{tableName}'
                        ";

            var  colmnsInfos = SqlHelper.ReaderToList<DBTableColmnsInfo>(BaseClass.ConnStr, sqlStr);


            (new CreateModel()).CreateModelInfo(this.tbxDirectoryPath.Text, tableName, this.tbxBaseClassName.Text, this.tbxNameSpace.Text, colmnsInfos);

            MessageBox.Show("保存成功！");
        }
    }
}
